<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>积分兑换</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no"/>
		<link rel="stylesheet" href="../css/mint-ui.css" />
		<link rel="stylesheet" href="../css/main.css" />
	</head>
	<body>
		<div id="app" style="overflow: hidden;">
			<div class="scores">
				当前剩余积分：{{score}}分 
				<span class="mainColor" @click="changeRecord" style="float:right;">
					兑换记录&nbsp;</span>
			</div>
			<div class="prizes" v-for="item in changes">
				<img :src="item.primaryPicUrl" />
				<strong class="mainColor">{{item.retailPrice}}分</strong><br />
				<mt-button @click="forChange(item.id)" type="primary" size="small" style="height:24px;margin:5px 0;">
					兑换</mt-button>
			</div>
			
		</div>
		<br /><br />
	</body>
	<script type="text/javascript" src="../js/vue.js" ></script>
	<script type="text/javascript" src="../js/mint-ui.js" ></script>
	<script type="text/javascript" src="../js/rem.js" ></script>
	<script type="text/javascript" src="../js/axios.min.js" ></script>
	<script>
		new Vue({
			el:"#app",
			data:{
				score:"",
				openId:"",
				changes:[
//					{id:1,src:"../images/picture.jpg",price:"600"},
//					{id:2,src:"../images/picture.jpg",price:"700"},
//					{id:3,src:"../images/picture.jpg",price:"400"},
				]
			},
			created(){
				this.openId=localStorage.getItem("openId");
				//获取积分
				axios.post('http://106.13.55.49/api/jdUser/userCenter?openid='+this.openId)
				.then((data) => {
					if(data.data.status == 200){
						this.score=data.data.data.useraccount.credits;
					}
				})
				//获取产品列表
				axios.get("http://106.13.55.49/api/jdGoods/list")
				.then(data => {
					console.log(data);
					if(data.data.status == 200){
						let datas=data.data.data;
						for(let i in datas){
							this.changes.push(datas[i]);
						}
					}else{
						this.$toast(data.data.message);return;
					}
					
				});
			},
			methods:{
				//兑换记录
				changeRecord:function(){
					location.href="changeRecords.html";
				},
				//兑换
				forChange:function(id){
					axios.post("http://106.13.55.49/api/jdGoods/exchange",{
						"goodId":id,
						"openid":this.openId
					}).then(data => {
					console.log(data);
					if(data.data.status == 200){
						this.$toast({message:"恭喜您兑换成功，请前往店内领取",duration:1500});
						setTimeout(function(){
							location.reload();
						},2000);
						
					}else{
						this.$toast(data.data.message);return;
					}
					
				});
				}
			}
			
		})
	</script>
</html>
